import Http from "../../network/Http";
import { JSBridge } from "../JSBridge";
import LoginBase from "../LoginBase";
import { TouTiaoConfig } from "./TouTiaoConfig";
import QuLe from "../QuLe";

export default class TouTiaoLogin extends LoginBase {

    readonly getOpenIdUrl = 'https://developer.toutiao.com/api/apps/jscode2session';

    /**
     * 登录
     * @param onFinish 成功获取用户信息返回参数true,否则false
     */
    login(onFinish: Function) {
        super.login(onFinish);
        this.onLoginFinish = onFinish;
        let tt = JSBridge.getTTObj();
        if (!tt) return;
        let _this = this;
        tt.login({
            force: true,//先尝试拉取登录框
            success(res) {
                console.log(`头条登录成功 ${res.code} ${res.anonymousCode}`);
                _this.session = res.code;
                _this.session2 = res.anonymousCode;
                _this.isLogined = true;
                if (_this.onLoginFinish) {
                    _this.onLoginFinish(true);
                }
            },
            fail(err) {
                //用户取消登录后在获取一次非登录的res.anonymousCode
                tt.login({
                    force: false,
                    success(res) {
                        console.log(`头条登录成功 ${res.code} ${res.anonymousCode}`);
                        _this.session = res.code;
                        _this.session2 = res.anonymousCode;
                        _this.isLogined = true;
                        if (_this.onLoginFinish) {
                            _this.onLoginFinish(true);
                        }
                    },
                    fail(err) {
                        console.error('头条登录失败: ' + JSON.stringify(err));
                        if (_this.onLoginFinish) {
                            _this.onLoginFinish(false);
                        }
                    }
                });
            }
        });
    }

    /** 获取头条OpenId */
    getOpenId(code: string, anonymous_code: string) {
        let params = 'appid=' + TouTiaoConfig.appId + '&secret=' + QuLe.instance.remoteConfig.channelSecret + '&code=' + code + '&anonymous_code=' + anonymous_code;
        console.log('开始获取头条OpenId: ' + (this.getOpenIdUrl + '?' + params));
        Http.get(this.getOpenIdUrl + '?' + params, (success: boolean, res: string) => {
            if (success) {
                let resObj = JSON.parse(res);
                if (resObj.error == 0) {
                    this.openid = resObj.openid;
                    this.session = resObj.anonymous_openid;
                    this.session2 = resObj.session_key;
                    if (this.onLoginFinish) {
                        this.onLoginFinish(true);
                    }
                } else {
                    if (this.onLoginFinish) {
                        this.onLoginFinish(false);
                    }
                }
            } else {
                if (this.onLoginFinish) {
                    this.onLoginFinish(false);
                }
            }

        })
    }

}
